Skip to content

Add Swift section with JSONCKit link#16

Merged
DecimalTurn merged 1 commit intoJSONC-org:mainfrom
steelbrain:patch-1
Mar 22, 2026
Merged

Add Swift section with JSONCKit link#16
DecimalTurn merged 1 commit intoJSONC-org:mainfrom
steelbrain:patch-1

Conversation

@steelbrain
Copy link
Contributor

@steelbrain steelbrain commented Mar 15, 2026

JSONCKit is heavily inspired by the https://github.com/marcozac/go-jsonc implementation, see ATTRIBUTIONS.

It borrows tests from a bunch of different libs in the ecosystem. I'm using it in some of my own macOS apps, happy to share it with the ecosystem.

Edit: Here's a very simple program that'd use it
~/P/t/jsonc-parser ❯❯❯ tree
.
├── Package.resolved
├── Package.swift
└── Sources
    └── jsonc-parser
        └── main.swift

3 directories, 3 files
~/P/t/jsonc-parser ❯❯❯ cat Sources/jsonc-parser/main.swift
import Foundation
import JSONCKit

let inputData = FileHandle.standardInput.readDataToEndOfFile()

guard !inputData.isEmpty else {
    exit(0)
}

do {
    let jsonData = try JSONC.convertToData(inputData)
    FileHandle.standardOutput.write(jsonData)
} catch {
    FileHandle.standardError.write("Error: \(error.localizedDescription)\n".data(using: .utf8)!)
    exit(1)
}
~/P/t/jsonc-parser ❯❯❯ cat Package.swift
// swift-tools-version: 6.0

import PackageDescription

let package = Package(
    name: "jsonc-parser",
    platforms: [.macOS(.v10_15)],
    dependencies: [
        .package(url: "https://github.com/steelbrain/JSONCKit.git", from: "1.0.0"),
    ],
    targets: [
        .executableTarget(
            name: "jsonc-parser",
            dependencies: [.product(name: "JSONCKit", package: "JSONCKit")]
        ),
    ]
)
~/P/t/jsonc-parser ❯❯❯ echo '{ /* comment */ "key": "value" }' | swift run jsonc-parser
[1/1] Planning build
Building for debugging...
[1/1] Write swift-version--58304C5D6DBC2206.txt
Build of product 'jsonc-parser' complete! (0.10s)
{               "key": "value" }

JSONCKit is heavily inspired by the https://github.com/marcozac/go-jsonc implementation, see [ATTRIBUTIONS](https://github.com/steelbrain/JSONCKit/blob/main/ATTRIBUTIONS.md).

It borrows tests from a bunch of different libs in the ecosystem. I'm using it in some of my own macOS apps, happy to share it with the ecosystem.
@DecimalTurn
Copy link
Member

Thanks for the PR. This could be a valuable ressource to add, however since it's not a parser nor a JSONC genertor, we should add it in a seperate category. Would you be ok with this being listed in a "JSONC to JSON Converter" category that would appear below the current list?

@steelbrain
Copy link
Contributor Author

Thanks for taking a look! It does support parsing/decoding but only in a typed manner to a shape directly: https://github.com/steelbrain/JSONCKit?tab=readme-ov-file#decode-a-decodable-type-directly

I'm happy with it being placed where you feel is appropriate! I've ticked "allow edits by maintainers", please let me know how I should change it if I am to make this change!

@DecimalTurn
Copy link
Member

It does support parsing/decoding but only in a typed manner to a shape directly:

I see, in that case, we can keep it among the other parser libraries.

One last thing before merging, can you confirm that JSONC.decode supports trailing commas by default?

@steelbrain
Copy link
Contributor Author

Confirmed! Tested the following json blob with trailing commas and it worked well

Source code
import Foundation
import JSONCKit

let jsonc = """
{
    // Server configuration
    "host": "localhost",
    "port": 8080,
    "features": [
        "auth",
        "logging",
        "metrics", // we may add more later
    ],
    "database": {
        "name": "mydb",
        "pool_size": 5,
        /* credentials loaded from env at runtime */
    },
}
"""

struct Database: Decodable {
    let name: String
    let pool_size: Int
}
struct Config: Decodable {
    let host: String
    let port: Int
    let features: [String]
    let database: Database
}

let config = try JSONC.decode(Config.self, from: Data(jsonc.utf8))

assert(config.host == "localhost")
assert(config.port == 8080)
assert(config.features == ["auth", "logging", "metrics"])
assert(config.database.name == "mydb")
assert(config.database.pool_size == 5)

print("Parsed config:")
print("  host: \(config.host)")
print("  port: \(config.port)")
print("  features: \(config.features)")
print("  database: \(config.database.name) (pool: \(config.database.pool_size))")

@DecimalTurn DecimalTurn merged commit 55d5b6a into JSONC-org:main Mar 22, 2026
@steelbrain steelbrain deleted the patch-1 branch March 22, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants